﻿using System;
using System.Data;
using System.Collections.Generic;

using Clipper.Model;
using System.Collections.ObjectModel;

namespace Clipper.BLL
{
    /// <summary>
    /// ReagentKit
    /// </summary>
    public partial class ReagentKit
    {
        private readonly Clipper.DAL.ReagentKit dal = new Clipper.DAL.ReagentKit();
        BLL.Reagent mReagentBll = new BLL.Reagent();
        public ReagentKit()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Clipper.Model.ReagentKit model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据,同时更新试剂状态
        /// </summary>
        public bool Update(Clipper.Model.ReagentKit model)
        {
            bool res1 = false, res2 = false;
            if (dal.Update(model))
            {
                if (model.ReagentModel1 != null)
                {
                    model.ReagentModel1.StateId = 2;
                    res1 = mReagentBll.Update(model.ReagentModel1);
                }
                if (model.ReagentModel2 != null)
                {
                    model.ReagentModel2.StateId = 2;
                    res2 = mReagentBll.Update(model.ReagentModel2);
                }
                else
                {
                    res2 = true;
                }
            }
            if (res1 && res2)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 卸载试剂
        /// </summary>
        /// <param name="model">试剂盒实体</param>
        /// <param name="kitOrder">试剂盒编号（1或2）</param>
        /// <returns></returns>
        public bool UnloadReagent(Model.ReagentKit model, int kitOrder)
        {
            bool res1 = false, res2 = false;
            if (kitOrder == 1)
            {
                if (model.ReagentModel1 != null)
                {
                    model.ReagentID1 = null;
                    model.ReagentModel1.StateId = 1;
                    model.loadDate1 = null;//给时间覆初始值

                    res1 = mReagentBll.Update(model.ReagentModel1);
                }
            }
            else if (kitOrder == 2)
            {
                if (model.ReagentModel2 != null)
                {
                    model.ReagentID2 = null;
                    model.ReagentModel2.StateId = 1;
                    res2 = mReagentBll.Update(model.ReagentModel2);
                }
            }
            if (res1 || res2)
            {
                return dal.Update(model);
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int holeNumber)
        {
            return dal.Delete(holeNumber);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Clipper.Model.ReagentKit GetModel(int holeNumber)
        {
            return dal.GetModel(holeNumber);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public ObservableCollection<Clipper.Model.ReagentKit> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得所有数据列表
        /// </summary>
        public ObservableCollection<Clipper.Model.ReagentKit> GetModelList()
        {
            DataSet ds = dal.GetList("");
            return DataTableToList(ds.Tables[0]);
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public ObservableCollection<Clipper.Model.ReagentKit> DataTableToList(DataTable dt)
        {
            ObservableCollection<Clipper.Model.ReagentKit> modelList = new ObservableCollection<Clipper.Model.ReagentKit>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Clipper.Model.ReagentKit model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        //填充试剂1、2实体
                        if (model.ReagentID1 != null)
                        {
                            Model.Reagent reagent1 = null;
                            reagent1 = mReagentBll.GetItemsReagent(model.ReagentID1.Value);
                            if (reagent1 != null)
                            {
                                model.ReagentModel1 = reagent1;
                            }
                        }
                        //if (model.ReagentID2 != null)
                        //{
                        //    Model.Reagent reagent2 = null;
                        //    reagent2 = mReagentBll.GetItemsReagent(model.ReagentID2.Value);
                        //    if (reagent2 != null)
                        //    {
                        //        model.ReagentModel2 = reagent2;
                        //    }
                        //}
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

